import pyscreeze
from loguru import logger
from tqdm import tqdm

import Config
from driver.driver import *
from public.public import *


def find_envoy_in_inn():
    """
    在客栈中查找“使者”
    :return:
    """
    img = "./imgs/envoy.jpg"
    try:
        return pyautogui.locateOnScreen(img, confidence=0.9)
    except BaseException:
        logger.error("双倍招兵校验错误，十秒重试")
        time.sleep(10)
        return None


def find_envoy_in_commanders():
    """
    在将军中查找“使者”
    :return:
    """
    img = "./imgs/envoy1.jpg"
    try:
        return pyautogui.locateOnScreen(img, confidence=0.9)
    except BaseException:
        logger.error("双倍招兵校验错误，十秒重试")
        time.sleep(10)
        return None


def entry_function():
    """
    入口函数
    :return:
    """
    control = get_control_by_level([0, 0, 0, 0, 0, 0], base_name="热血三国-")
    left, top, width, height = get_control_position(control)

    sleep_time = get_sleep_time(control)
    cities = get_all_city(control)

    while True:
        for city in cities:
            entry_city(left, top, city[0], city[1])
            time.sleep(sleep_time)
            # 点击客栈
            _ = Config.ChengNeiConfig.inn
            mouse_move_to_and_click(left + _[0], top + _[1])
            time.sleep(sleep_time)

            _ = find_envoy_in_inn()
            if _ is not None:
                _ = pyscreeze.center(_)
                mouse_move_to_and_click(_[0] + 540, _[1])
                time.sleep(sleep_time)
                print(_)
                # 点击招募按钮
                _ = Config.Inn.ZhaoMuButton
                mouse_move_to_and_click(left + _[0], top + _[1])
                time.sleep(sleep_time)

                # 点击关闭按钮
                _ = Config.Inn.CloseButton
                mouse_move_to_and_click(left + _[0], top + _[1])
                time.sleep(sleep_time)

                # 点击将领
                _ = Config.MainConfig.Commanders
                mouse_move_to_and_click(left + _[0], top + _[1])
                time.sleep(sleep_time)

                # 查找使者
                _ = find_envoy_in_commanders()
                _ = pyscreeze.center(_)

                # 点击使者
                mouse_move_to_and_click(_[0], _[1])
                time.sleep(sleep_time)

                # 点击解雇
                _ = Config.Commanders.FireButton
                mouse_move_to_and_click(left + _[0], top + _[1])
                time.sleep(sleep_time)

                # 点击确认按钮
                _ = Config.Commanders.SureButton
                mouse_move_to_and_click(left + _[0], top + _[1])
                time.sleep(sleep_time)

                # 点击关闭按钮
                _ = Config.Commanders.CloseButton
                mouse_move_to_and_click(left + _[0], top + _[1])
                time.sleep(sleep_time)

            else:
                # 关闭客栈按钮
                _ = Config.Inn.CloseButton
                mouse_move_to_and_click(left + _[0], top + _[1])
                time.sleep(sleep_time)

        for i in tqdm(range(1 * 60 * 60)):
            time.sleep(1)


if __name__ == '__main__':
    entry_function()
    # _ = find_envoy_in_commanders()
    # _ = pyscreeze.center(_)
    # print(_)